package com.ton.utils;

import org.assertj.core.util.Lists;
import org.thymeleaf.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    //获取当前年月
    public static String getYearMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Calendar calBegin = Calendar.getInstance();
        calBegin.set(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calBegin.getTime());
    }

    public static String getYearMonthString(String strDate) {
        // 解析日期字符串为 LocalDate 对象
        LocalDate date = LocalDate.parse(strDate, DateTimeFormatter.ISO_DATE);
        // 格式化为年月字符串
        String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyyMM"));
        return formattedDate;
    }

    public static String getMonthString(Date startDate) {
        // Define the date format you want
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");

        // Convert the Date to a String
        String dateString = dateFormat.format(startDate);
        // 格式化为年月字符串
        return dateString;
    }

    public static String getDayString(Date startDate) {
        // Define the date format you want
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        // Convert the Date to a String
        String dateString = dateFormat.format(startDate);
        // 格式化为年月字符串
        return dateString;
    }

    public static String getDayBegin(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        return date + " 00:00:00";
    }

    /**
     * 时间戳转换年月日
     *
     * @param date
     * @return
     */
    public static Date getYearMonthDay(Long timestamp) throws ParseException {
        if (timestamp == null || timestamp == 0L) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(timestamp);
        return format.parse(date);
    }

    public static String getDayEnd(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        return date + " 23:59:59";
    }

    /**
     * 获取当月第一天
     *
     * @return
     */
    public static String getMonthBegin() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calBegin = Calendar.getInstance();
        calBegin.set(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calBegin.getTime());
    }

    /**
     * 获取当前日期和前一天日期
     *
     * @return
     */
    public static List<String> getNowAndYesterday() {
        List<String> dates = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        dates.add(sdf.format(date));
        dates.add(sdf.format(new Date(date.getTime() - 24 * 60 * 60 * 1000)));
        return dates;
    }

    /**
     * 获取当月最后一天
     *
     * @return
     */
    public static String getMonthEnd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calEnd = Calendar.getInstance();
        calEnd.set(Calendar.DAY_OF_MONTH, calEnd.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sdf.format(calEnd.getTime());
    }

    public static Date addDay(Date date, int addDay) {
        long day = 86400000L;
        long time = date.getTime() + day * (long) addDay;
        return new Date(time);
    }

    public static Integer getDateInteger() {
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(new Date());
        if (calBegin.get(Calendar.MONTH) + 1 < 10) {
            if (calBegin.get(Calendar.DAY_OF_MONTH) < 10) {
                return Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "0" + (calBegin.get(Calendar.DAY_OF_MONTH)));
            } else {
                return Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH)));
            }
        } else {
            return Integer.valueOf((calBegin.get(Calendar.YEAR)) + "" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH)));
        }
    }

    public static List<Integer> getDateList(String beginTime, String endTime) {
        List<Integer> result = Lists.newArrayList();
        if (StringUtils.isEmpty(beginTime) || StringUtils.isEmpty(endTime)) {
            return result;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;
        try {
            beginDate = sdf.parse(beginTime);
            endDate = sdf.parse(endTime);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(beginDate);
        if (calBegin.get(Calendar.MONTH) + 1 < 10) {
            if (calBegin.get(Calendar.DAY_OF_MONTH) < 10) {
                result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "0" + (calBegin.get(Calendar.DAY_OF_MONTH))));
            } else {
                result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH))));
            }
        } else {
            result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH))));
        }

        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endDate);

        while (endDate.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            if (calBegin.get(Calendar.MONTH) + 1 < 10) {
                if (calBegin.get(Calendar.DAY_OF_MONTH) < 10) {
                    result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "0" + (calBegin.get(Calendar.DAY_OF_MONTH))));
                } else {
                    result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "0" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH))));
                }
            } else {
                result.add(Integer.valueOf((calBegin.get(Calendar.YEAR)) + "" + (calBegin.get(Calendar.MONTH) + 1) + "" + (calBegin.get(Calendar.DAY_OF_MONTH))));
            }
        }
        return result;
    }

    public static String getPastDate(int past, String dateStr) {
        past -= 1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        String result = sdf.format(today);
        return result;
    }

    public static List<String> getDatesBetween(Date startDate, Date endDate) {
        List<String> dates = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        while (cal.getTime().before(endDate) || cal.getTime().equals(endDate)) {
            String dateString = dateFormat.format(cal.getTime());
            dates.add(dateString);
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dates;
    }
}
